package com.xiaodimall.beforesys.service.impl;

import com.xiaodimall.beforesys.mapper.GoodsMapper;
import com.xiaodimall.beforesys.pojo.Goods;
import com.xiaodimall.beforesys.service.GoodsService;
import com.xiaodimall.common.Constants;
import com.xiaodimall.common.ServiceResultEnum;
import com.xiaodimall.exception.MallException;
import com.xiaodimall.utils.BeanUtil;
import com.xiaodimall.utils.PageQueryUtil;
import com.xiaodimall.utils.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wyc
 * @description
 * @ClassName GoodsServiceImpl
 * @date 2022/11/26 10:40
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    //注入持久层接口
    @Resource
    GoodsMapper goodsMapper;

    /**
     * 根据商品id查看商品详情
     * @author WYC
     * @param goodsId
     * @param request
     * @return: 商品详情页面mall/detail
     * @date: 2022/11/28 10:13
     */
    @Override
    public String findByIdGoodsMsg(Long goodsId, HttpServletRequest request) {

        if (goodsId < 1) {
            return "error/error_5xx";
        }
        //根据id查询商品
        Goods goods = goodsMapper.selectById(goodsId);
        //判断商品是否哦为空
        if (goods == null) {
            MallException.fail(ServiceResultEnum.GOODS_NOT_EXIST.getResult());
        }
        //判断商品是否下架
        if (Constants.SELL_STATUS_UP != goods.getGoodsSellStatus()) {
            MallException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
        }
        request.setAttribute("goodsDetail", goods);
        return "mall/detail";
    }


    /**
     * 根据用户输入的信息查询出相关的商品
     * @author WYC
     * @param params
     * @param request
     * @return: 搜索页面
     * @date: 2022/12/1 20:11
     */
    @Override
    public String searchGoods(Map<String, Object> params, HttpServletRequest request) {
        //设置分页
        System.out.println("params = " + params);
        if (StringUtils.isEmpty(params.get("page"))) {
            params.put("page", 1);
        }
        params.put("limit", Constants.GOODS_SEARCH_PAGE_LIMIT);
        String keyword = "";
        //对keyword做过滤 去掉空格
        if (params.containsKey("keyword") && !StringUtils.isEmpty((params.get("keyword") + "").trim())) {
            keyword = params.get("keyword") + "";
        }
        request.setAttribute("keyword", keyword);
        params.put("keyword", keyword);
        //搜索上架状态下的商品
        params.put("goodsSellStatus", Constants.SELL_STATUS_UP);
        //封装商品数据
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        //模糊查询并进行排序
        List<Goods> goodsList = goodsMapper.findGoodsListBySearch(pageUtil);
        //分页处理
        int total = goodsMapper.getTotalGoodsBySearch(pageUtil);
        //处理字符串过长导致文字超出的问题
        if (!CollectionUtils.isEmpty(goodsList)) {
            for (Goods goods : goodsList) {
                String goodsName = goods.getGoodsName();
                String goodsIntro = goods.getGoodsIntro();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 28) {
                    goodsName = goodsName.substring(0, 28) + "...";
                    goods.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 30) {
                    goodsIntro = goodsIntro.substring(0, 30) + "...";
                    goods.setGoodsIntro(goodsIntro);
                }
            }
        }
        PageResult pageResult = new PageResult(goodsList, total, pageUtil.getLimit(), pageUtil.getPage());
        //绑定查询出的数据
        request.setAttribute("pageResult", pageResult);
        return "mall/search";
    }

}
